import {defineStore} from 'pinia'

export default defineStore('user', {
    state: () => ({
        token: '',
        user: {},
        rememberMeObj: {
            rememberMe: false,
            account: '',
            password: ''
        },
    }),
    actions: {
        setToken(token){
            this.token = token
        },
        setUser(user){
            this.user = user
        },
        setAuthInfo(token, user){
            this.token = token
            this.user = user
        },
        setRememberMe(account, password){
            this.rememberMeObj.rememberMe = true
            this.rememberMeObj.account = account
            this.rememberMeObj.password = password
        },
        clearRememberMe(){
            this.rememberMeObj = {
                rememberMe: false,
                account: '',
                password: ''
            }
        }
    },
    persist: {
        enabled: true,
        strategies: [
            {
                key: "USER-DATA",
                storage: process.client ? localStorage : null,
            },
            {
                key: "REMEMBER_ME-DATA",
                storage: process.client ? localStorage : null,
            },
        ],
    }
})